const commonUrl ="https://qiuzhi.lalaluowang.club/"  //公共路径
 
// post请求封装
function postRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': uni.getStorageSync("token")
			},
			success: function(res) {
                //res.data.data也根据自己的后台返回层级决定
				if (res.statusCode == 200 && res.data.code == 0) {
					resolve(res.data);
				} else {
					// 请求服务器成功，但是由于服务器没有数据返回
					resolve(res.data);
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}
 
// get请求封装
function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: 'GET',
			header: {
				'content-type': 'application/json',
				'token': uni.getStorageSync("token")
			},
			success: function(res) {
				if (res.statusCode == 200 && res.data.code == 0) {
					resolve(res.data);
				} else {
						resolve(res.data);
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}
 
module.exports = {
	postRequest,
	getRequest,
	commonUrl
}